<?php

/**
 * 微博群组管理页
 * author yangchao
 * date 2011/05/04
 */

include_once($_SERVER["DOCUMENT_ROOT"] . "/core/inc.php");
include_once($_SERVER["DOCUMENT_ROOT"] . "/core/admin.inc.php");
include_once($_SERVER["DOCUMENT_ROOT"] . "/include/foodv2/inc.php");

function check_cgi_pro()
{		
	global $g_cgival, $g_pro, $g_show, $config;
	
	//check refer  -  add by xinwei
	if(! security_check::check_refer())
	{
		throw new Exception("来路不明");
	}
	
	cgi::both($g_cgival['group_id'],"group_id",0);
	$g_cgival['group_id'] = intval($g_cgival['group_id']);
	
	cgi::both($g_cgival['msg_title'],"msg_title","美食消息");
	$g_cgival['msg_title'] = string::un_script_code($g_cgival['msg_title']);
	$g_cgival['msg_title'] = string::un_html($g_cgival['msg_title']);

	cgi::both($g_cgival['msg_content'],"msg_content","");
	$g_cgival['msg_content'] = string::un_script_code($g_cgival['msg_content']);
	$g_cgival['msg_content'] = string::un_html($g_cgival['msg_content']);
	if(ceil(strlen(preg_replace("/&(#\d{5});/", "__", iconv('UTF-8', 'GBK', $g_cgival['msg_content'])))/2)>300)
	{
		throw new Exception("内容长度请小于290字。".strlen($g_cgival['msg_content']));
	}
	
	cgi::both($g_cgival['msg_url'],"msg_url","");
	$g_cgival['msg_url'] = string::un_script_code($g_cgival['msg_url']);
	$g_cgival['msg_url'] = string::un_html($g_cgival['msg_url']);
	
	cgi::both($g_cgival['to_uid'],"to_uid","");
	$g_cgival['to_uid'] = string::un_script_code($g_cgival['to_uid']);
	
}
function get_data()
{
	global $g_cgival, $g_pro, $g_show, $pager, $config;
	
	$mblog = new mblog_from_api();
	$admin_log = new admin_log();
	
	$site_id = admin::admin_get_user_site_id();
	$product_id = 120;	//吃客前台产品ID
	$admin_id = admin::admin_get_login_uid();
	$admin_name = admin::admin_get_login_user_name();
	$group_id = $g_cgival['group_id'];
		
	$uid_array = array();
	$g_cgival['to_uid'] = trim($g_cgival['to_uid']);
	
	$get_uids_rule="";
	if($group_id)
	{
		$msg_target = 0;
		$get_uids_rule ="http://group.henan.sina.com.cn/api/group/api_get_uid_list.php?group_id={$group_id}";
		
	}
	elseif(!$group_id && !$g_cgival['to_uid'])
	{
		//当前群列表
		$msg_target = 0;
		$get_uids_rule ="http://group.henan.sina.com.cn/api/group/api_get_uid_list.php?site_id={$site_id}";
	
	}
	else
	{
		$msg_target = $g_cgival['to_uid'];
		$group_id = 0;
	}
	
	//调用api
	$api_server	= "http://user.henan.sina.com.cn/";// $config["user_center_domain"][$config["current_site_id"]];
	$app_key 	= $config["user_center_app"][$config["current_site_id"]][$config["current_product_id"]]["app_key"];
	$app_secret	= $config["user_center_app"][$config["current_site_id"]][$config["current_product_id"]]["app_secret"];

	$uid = $g_pro['uid'];
	$action_id = 1;
	$product_id = 120;//群组
	$msg_title = $g_cgival['msg_title'];
	$msg_content = $g_cgival['msg_content'];
	$urlArr = $mblog->api_url_to_short($g_cgival['msg_url'],'');
	$msg_url = $urlArr['data']['result'][0]['url_short'];
	
	$api = new sina_dfz_lib($app_key,$app_secret,$api_server);
	$data=$api->api_add_message_v2($admin_id,$msg_target,$msg_title,$msg_content,$msg_url,$site_id,$product_id,$group_id);

//	$msg = new mod_food_private_message(); 
//	$data = $msg->add_notice($g_cgival['to_uid'],$g_cgival['msg_content'],$admin_id);
	
	$data1 = json_decode($data,1);

	if($data1["error"] == 1)
	{
		throw new Exception($data1["errmsg"]);
	}
	
	$admin_log->admin_insert_log_info($admin_id, $site_id, 17, "新增 社区通知");
}
try
{
	
    admin_check_login();
	admin_check_user_priv("foodv2/message/user_sys_message_add_post.php");
	check_cgi_pro();
	get_data();

	$g_show["error"] = "0";
	$g_show["errmsg"] = "";
}
catch (Exception $e)
{
	$g_show["error"] = "1";
	$g_show["errmsg"] = $e->getMessage();
}
show_pro();
exit();

function show_pro()
{
	global $g_cgival, $g_pro, $g_show, $smarty, $pager, $config;

	echo json_encode($g_show);

}

?>